const app = getApp();
const publicFun = require('/utils/public.js');
import { userNGOList, userNGOModify } from '/utils/request'

Page({
  data: {
    NGOList: [],
    keywordContent: '',
  },
  onLoad() {
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
    this.setData({ 
      NGOList: [], 
      systemInfo: app.globalData.systemInfo 
    });
    this.userNGOList();
  },
  keywordVal(e) {
    this.setData({
      keywordContent: e.detail.value,
    });
    if(e.detail.value.length == 0){
      this.userNGOList();
    }
  },
  async userNGOList() {
    let that = this;
    try {
      let params = {
        keyword: this.data.keywordContent
      }
      my.showLoading();
      const res = await userNGOList(params)
      if (res.S == 1) {
        my.hideLoading();
        if(res.L.length > 0){
          if(this.data.keywordContent){
            that.setData({ NGOList: [] });
          }else{
            that.setData({ NGOList: [ [0, '无', '0', ''] ] });
          }
          res.L.forEach((item) => {   
            that.data.NGOList.push(item);
          });
          that.setData({
            NGOList: that.data.NGOList
          })
        }else{
          that.setData({ NGOList: [] });
        }
      }else{
        my.hideLoading();
        publicFun.showToast(res.M);
      }
    } catch (e) {
      console.log(e)
    }
  },
  jumpNGODetail(e) {
    let data = e.currentTarget.dataset;
    if(data.id == 0) {
      this.NGOModify(data.id);
    }else{
      my.navigateTo({
        url: '../myOrganDetail/myOrganDetail?ngoId='+data.id
      })
    }
  },
  async NGOModify(ngoid) {
    let that = this;
    try {
      let params = {
        UserId: app.globalData.userid,
        ngoid: ngoid,
        type: 1,
        teamid: 0
      }
      my.showLoading();
      const res = await userNGOModify(params)
      if (res.S == 1) {
        my.hideLoading();
        publicFun.showToast(res.M);
        app.getUserData();
        setTimeout(() => {
          my.switchTab({
            url: '/pages/my/my',
          });
        }, 500)
      }else{
        my.hideLoading();
        publicFun.showToast(res.M);
      }
    } catch (e) {
      console.log(e)
    }
  },
  // onReachBottom() {
  //   console.log('加载')
  //   const newPage = this.data.pageindex + 1;
  //   this.userNGOList(newPage);
  //   this.setData({pageindex: newPage});
  // }
});
